﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class ProductSearch : SkinnedWebControl
    {
        private TextBox _tbxSearchKey;
        private TreeCallBackDropDownList _ddlCategory;
        private TextBox _tbxBeginDate;
        private TextBox _tbxEndDate;
        private Button _btnConfirm;

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            _tbxSearchKey = (TextBox)skin.FindControl("tbxSearchKey");
            _tbxBeginDate = (TextBox)skin.FindControl("beginDate");
            _tbxEndDate = (TextBox)skin.FindControl("endDate");
            _ddlCategory = (TreeCallBackDropDownList)skin.FindControl("ddlCategory");
            _btnConfirm = (Button)skin.FindControl("btnConfirm");

            _ddlCategory.ShowBaseNode = false;
            _ddlCategory.BaseNodeId = SiteContext.Current.BaseNodeId;

            //这里必须是!Page.IsPostBack
            if (!Page.IsPostBack)
            {
                if (_btnConfirm != null)
                    _btnConfirm.Click += new EventHandler(_btnConfirm_Click);

                if (_ddlCategory != null)
                    _ddlCategory.SelectValue = SiteContext.Current.CategoryId;

                if (_tbxSearchKey != null)
                    _tbxSearchKey.Text = SiteContext.Current.SearchKey;

                if (BeginDate != DateTime.MinValue && _tbxBeginDate != null)
                    _tbxBeginDate.Text = Globals.FormatDateTime(BeginDate, DateTimeFormatMode.Date);

                if (EndDate != DateTime.MinValue && _tbxEndDate != null)
                    _tbxEndDate.Text = Globals.FormatDateTime(EndDate, DateTimeFormatMode.Date);
            }

            _btnConfirm.Click+=new EventHandler(_btnConfirm_Click);
        }

        void _btnConfirm_Click(object sender, EventArgs e)
        {
            string url = SiteUrls.Urls.ProductSearchAdmin(SiteContext.Current.EnableDelButton, SiteContext.Current.EnableTopButton, SiteContext.Current.EnableHotButton, SiteContext.Current.BaseNodeId, (StatusType)SiteContext.Current.Status, CategoryId, KeyWord);
            SiteContext.Redirect(url);
        
        }

        private string KeyWord
        {
            get
            {
                string keyWord = string.Empty;
                if (_tbxSearchKey != null && !string.IsNullOrEmpty(_tbxSearchKey.Text.Trim()))
                    keyWord = _tbxSearchKey.Text.Trim();
                return keyWord;
            }
        }

        private int CategoryId
        {
            get
            {
                int cityId = -1;
                if (_ddlCategory != null)
                    cityId = _ddlCategory.SelectValue;
                return cityId;
            }
        }

        private DateTime BeginDate
        {
            get
            {
                if (_tbxBeginDate != null)
                    return Convert.ToDateTime(_tbxBeginDate.Text.Trim());
                else
                    return DateTime.MinValue;
            }
        }

        private DateTime EndDate
        {
            get
            {
                if (_tbxEndDate != null)
                    return Convert.ToDateTime(_tbxEndDate.Text.Trim());
                else
                    return DateTime.MinValue;
            }
        }

        protected override string DefaultSkinFilename
        {
            get { return "Products/ProductSearch.ascx"; }
        }
    }
}
